<?php
//日本語
class FriendsController extends AppController {

	var $name = 'Friends';
	var $uses = array('Friend', 'User', 'Message', 'Transact');
	var $scaffold;
	var $components = array('AdminAuth', 'Pmaimai', 'RequestHandler', 'Token', 'Name');
	var $protectedActions = array('index', 'add', 'edit', 'delete', 'view');
	var $helpers = array('Html', 'Form');
	
	function accept()
	{
		$this->Pmaimai->checkMemberAuth();
		
		$this->set('error', false);
		$userID = $this->Session->read($this->sessionUserName);
		//GETのデータはPOSTに入れちゃう　入力エラー再表示時対策
		if(empty($this->data['Friend'])){
			$this->data['Friend'] = array(
				'id' => $this->params['pass'][0],
				'token' => $this->params['pass'][1],
			);
		}
		$bindfriends = array(
			'belongsTo' => array(
				'User' => $this->Friend->belongsTo['User'],
			),
		);
		$this->Friend->unbindFully();
		$this->Friend->bindModel($bindfriends);
		$this->Friend->recursive = 1;
		
		$offer = $this->Friend->findByIdAndStatus($this->data['Friend']['id'], 'notActive');
		if(!$offer || $offer['Friend']['userfriend'] !== $userID){
			$this->flash('このキーでは承認できません' , '/');
			return;
		}
		
		$this->set('offer', $offer);
		$this->Friend->set($offer);
		
		if(!$this->Friend->checkActivate($this->data['Friend']['token'])){
			$this->flash(current($this->Friend->validationErrors) , '/');
			return;
		}
		
		$this->User->recursive = 0;
		$user = $this->User->findById($userID);
		$this->set('user', $user);
		
		if(!isset($this->data['Friend']['agree'])){
			return;
		}
		
		if(empty($this->data['Friend']['agree']) || $this->data['Friend']['agree'] !== '1'){
			$this->Friend->invalidate('agree', 'なんかへんです');
		}
		if(empty($this->data['Friend']['content'])){
			$this->Friend->invalidate('empty', '未入力箇所があります');
		}
		
		$this->Transact->begin();
		//オファーをアクティベート
		if(!$this->Friend->updateStatus('activate')){
			$this->Transact->rollback();
			$this->set('error', true);
			$this->set('errorMsg', $this->Friend->validationErrors);
			return;
		}
		
		//自分→相手をアクティベート済みで登録
		$friend = new Friend();
		if(!$friend->register($userID, $offer['Friend']['user_id'], 'activated')){
			$this->Transact->rollback();
			$this->set('error', true);
			$this->set('errorMsg', $friend->validationErrors);
			return;
		}
		
		//マイフレンド承認したよ！メッセージを送る
		$title = 'マイフレンドのオファー承認';
		$this->set('content', $this->data['Friend']['content']);
		ob_start();
		$this->render(null, '/message', '/message/friend_accepter');
		$content = ob_get_clean();
		
		if(!$this->Message->post($userID, $offer['Friend']['user_id'], $title, $content)){
			$this->Transact->rollback();
			$this->set('error', true);
			$this->set('errorMsg', $this->Message->validationErrors);
			return;
		}
		
		$this->Transact->commit();
		$this->flash(hq($offer['User']['nickname'].'さんのマイフレンドのオファーを承認しました'), array('controller' => 'messages', 'action' => 'sendbox'));
	}
	
	function offer($id = null)
	{
		$this->Pmaimai->clean($id);
		$this->Pmaimai->checkMemberAuth();
		
		if(!$id || !is_numeric($id)){
			$this->redirect('/');
		}
		
		$this->set('error', false);
		$userID = $this->Session->read($this->sessionUserName);
		$this->User->recursive = 0;
		$user = $this->User->findById($userID);
		$this->set('user', $user);
		
		$this->User->recursive = 0;
		$other = $this->User->findByIdAndStatus($id, 'active');
		if(!$other || $other['User']['id'] === $userID){
			$this->flash("オファーできません", '/');
			return;
		}
		$this->set('other', $other);
		$cond = array('Friend.user_id' => '= '.$userID, 'Friend.userfriend' => '= '.$id, 'Friend.status' => '= active');
		if($this->Friend->find($cond)){
			$this->flash("既にマイフレンド済みです", '/');
			return;
		}
		
		if(!isset($this->data['Friend'])){
			return;
		}
		
		if(empty($this->data['Friend']['content'])){
			$this->Friend->invalidate('empty', '未入力箇所があります');
		}
		
		$this->Transact->begin();
		//マイフレンドにnotActiveで登録
		if(!$this->Friend->register($userID, $other['User']['id'])){
			$this->Transact->rollback();
			$this->set('error', true);
			$this->set('errorMsg', $this->Friend->validationErrors);
			return;
		}
		$this->Friend->unbindFully();
		$friend = $this->Friend->read();
		$this->set('friend', $friend);
		
		//マイフレンドになって！メッセージを送る
		$title = 'マイフレンドのオファー';
		$this->set('content', $this->data['Friend']['content']);
		ob_start();
		$this->render(null, '/message', '/message/friend_receiver');
		$content = ob_get_clean();
		
		if(!$this->Message->post($userID, $other['User']['id'], $title, $content)){
			$this->Transact->rollback();
			$this->set('error', true);
			$this->set('errorMsg', $this->Message->validationErrors);
			return;
		}
		
		$this->Transact->commit();
		$this->flash(hq($other['User']['nickname'].'さんにマイフレンドのオファーを出しました'), array('controller' => 'messages', 'action' => 'sendbox'));
	}

}
?>